//
//  LXLocationViewController.m
//  LXLocationRelated
//
//  Created by Livespro on 2017/10/22.
//  Copyright © 2017年 Livesxu. All rights reserved.
//

#import "LXLocationViewController.h"

#import "AlertBlockCustom.h"
#import "LXHandleDatas.h"

#import "ShowTipView.h"

#import "LXLocationAction.h"
#import "LXMotionAction.h"

#import "LXDistanceDescription.h"
#import "LXMotionDescription.h"
#import "NSData+CBDGZIP.h"

#import "LXMapDetailViewController.h"

#import "LXLocationDistances.h"

#import <CommonCrypto/CommonDigest.h>

@interface LXLocationViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableTemp;

@property (nonatomic, strong) AlertBlockCustom *alert;

@property (nonatomic, strong) ShowTipView *rollView;

@property (nonatomic, strong) ShowTipView *pitchView;

@property (nonatomic, strong) ShowTipView *yawView;

@property (nonatomic, strong) ShowTipView *accelerationXView;

@property (nonatomic, strong) ShowTipView *accelerationYView;

@property (nonatomic, strong) ShowTipView *accelerationZView;

@property (nonatomic, strong) ShowTipView *gyroscopeXView;

@property (nonatomic, strong) ShowTipView *gyroscopeYView;

@property (nonatomic, strong) ShowTipView *gyroscopeZView;

@property (nonatomic, strong) ShowTipView *latitudeView;

@property (nonatomic, strong) ShowTipView *longitudeView;

@property (nonatomic, strong) ShowTipView *courseView;

@property (nonatomic, strong) ShowTipView *speedView;

@property (nonatomic, strong) NSDate *startDate;

@property (nonatomic, strong) NSDate *endDate;

@end

@implementation LXLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableTemp];
}

-(UITableView *)tableTemp{
    if (!_tableTemp) {
        _tableTemp=[[UITableView alloc]initWithFrame:CGRectMake(0, 20,[UIScreen mainScreen].bounds.size.width,44 *4) style:UITableViewStylePlain];
        
        _tableTemp.delegate = self;
        _tableTemp.dataSource = self;
        _tableTemp.showsHorizontalScrollIndicator = NO;
        _tableTemp.showsVerticalScrollIndicator  = NO;
        _tableTemp.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableTemp.backgroundColor=[UIColor whiteColor];
        
    }
    return _tableTemp;
}

#pragma mark - tableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    
    return 4;
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"查看运行记录";
            break;
        case 1:
            cell.textLabel.text = @"查看10分钟行程";
            break;
        case 2:
            cell.textLabel.text = @"查看10分钟传感器数据";
            break;
        case 3:
            cell.textLabel.text = @"查看行程数据";
            break;
            
        default:
            break;
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *monitors;
    switch (indexPath.row) {
        case 0:
            monitors = [[LXHandleDatas shareHandle] handleMonitorOut];
            break;
        case 1:

            monitors = [[LXHandleDatas shareHandle] handleLocationOutStart:[self tenMinBefor] End:[NSDate date]];
            break;
        case 2:
            monitors = [[LXHandleDatas shareHandle] handleMotionOutStart:[self tenMinBefor] End:[NSDate date]];
            break;
        case 3:
            monitors = [[LXHandleDatas shareHandle] handleDistanceOut];
            break;

        default:
            break;
    }

    monitors = monitors ? monitors : @"";

    [self.alert setValue:monitors forKey:@"messageText"];

    [self.alert show];
    
    RLMResults *locationDatas = [LXLocationDistances allObjects];// 从默认的 Realm 数据库中
    
    LXLocationDistances *lastDistance = locationDatas.lastObject;
    
    if (lastDistance) {
        
        LXMapDetailViewController *map = [[LXMapDetailViewController alloc]init];
        map.isLocalDatas = YES;
        map.startDate = lastDistance.locationStart;
        map.endDate = lastDistance.locationEnd;
        
        [self.navigationController pushViewController:map animated:YES];
    }
}

- (NSDate *)tenMinBefor{
    
    NSTimeInterval time = [[NSDate date] timeIntervalSinceReferenceDate];
    
    return [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:time - 10*60];
}

- (AlertBlockCustom *)alert{
    if (!_alert) {
        
//        __weak typeof(self) weakSelf = self;
        _alert = [[AlertBlockCustom alloc]initWithTitle:@"" message:@"" cancelButtonTitle:@"取消" clickBlockAction:^(AlertBlockCustom *alertView, NSInteger buttonIndex) {
            
            if (buttonIndex == 1) {
                
            }
            
        } otherButtonTitles:@"确定", nil];
    }
    return _alert;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
